<template>
    <div>
        <div style="line-height: 30px;margin-bottom: 4px;background-color: #ffffff;font-size: 18px;padding: 10px 30px 10px 30px">
            项目管理申报系统
            <el-button type="primary" style="float: right; padding: 5px 15px!important;margin-top: 4px"
                       icon="el-icon-edit"></el-button>
        </div>
        <el-row style="margin-top: 20px">
            <!--            <el-col :span="4">-->
            <el-tabs :tab-position="tabPosition" style="height: 800px;">
                <el-tab-pane label="所有成员">
                    <div class="container">
                        <div class="handle-box">
                            <el-button
                                    type="primary"
                                    icon="el-icon-plus"
                                    class="handle-del mr10"
                                    @click="toggleModal"
                            >邀请新成员
                            </el-button>

                            <el-button
                                    type="primary"
                                    icon="el-icon-delete"
                                    class="handle-del mr10"
                                    @click="delAllSelection"
                            >删除成员
                            </el-button>
                            <el-input v-model="query.name" placeholder="输入用户名查找成员" class="handle-input mr10"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                        </div>
                        <el-table
                                :data="tableData"
                                border
                                class="table"
                                ref="multipleTable"
                                header-cell-class-name="table-header"
                                @selection-change="handleSelectionChange"
                        >
                            <el-table-column type="selection" width="55" align="center"></el-table-column>
                            <el-table-column prop="realname" label="用户名"></el-table-column>
                            <el-table-column prop="userPhone" label="手机号"></el-table-column>
                            <el-table-column prop="role" label="职务(role)"></el-table-column>
                            <el-table-column prop="team" label="项目组（team）"></el-table-column>
                            <el-table-column label="操作" width="180" align="center">
                                <template slot-scope="scope">
                                    <el-button
                                            type="text"
                                            icon="el-icon-delete"
                                            class="blue"
                                            @click="handleDelete(scope.$index, scope.row)"
                                    >删除
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="pagination">
                            <el-pagination
                                    background
                                    layout="total, prev, pager, next"
                                    :current-page="query.pageIndex"
                                    :page-size="query.pageSize"
                                    :total="pageTotal"
                                    @current-change="handlePageChange"
                            ></el-pagination>
                        </div>
                    </div>
                </el-tab-pane>
            </el-tabs>
            <el-dialog :modal=false title="添加项目成员" :visible.sync="isAddMember">
                <el-form :model="form">
                    <!--                    职务暂时不选-->
                    <!--                    <el-form-item label="职务">-->
                    <!--                        <el-select v-model="form.region" placeholder="选择项目中担任的职务">-->
                    <!--                            <el-option label="前端" value="qd"></el-option>-->
                    <!--                            <el-option label="后台" value="ht"></el-option>-->
                    <!--                            <el-option label="设计" value="sj"></el-option>-->
                    <!--                        </el-select>-->
                    <!--                    </el-form-item>-->
                    <el-form-item label="用户id或手机号">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="isAddMember = false">取 消</el-button>
                    <el-button type="primary" @click="addMember">确 定</el-button>
                </div>
            </el-dialog>
        </el-row>
    </div>
</template>

<script>
    import { getMember, memberAdd, memberRemove } from '../../../api/project/member';

    export default {
        name: 'basetable',
        data() {
            return {
                // is AddMember 判断当前是否显示弹框
                isAddMember: false,
                tabPosition: 'left',
                query: {
                    projectId: null,
                    offset: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 发送邀请用户请求
            // todo 确认用户职务
            addMember() {
                let that = this;
                memberAdd({
                    projectId: this.$store.state.projectId || this.$route.query.projectId,
                    userPhone: this.form.phone
                }).then(res => {
                    this.$message.success({
                        title: '邀请成功!',
                        duration: 1000,
                        onClose: location.reload()
                    });
                });
                this.isAddMember = false;
            },
            // 删除用户数据请求
            delMember(phone,next) {
                memberRemove({
                    projectId: this.$store.state.currentProject || this.$route.query.projectId,
                    userPhone: phone
                }).then(res => {
                    next();
                });
            },
            // 显示编辑弹框
            toggleModal() {
                this.isAddMember = true;
            },
            // 获取成员列表
            getData() {
                getMember({ projectId: this.$store.state.currentProject || this.$route.query.projectId }).then(res => {
                    this.tableData = res.data;
                    this.pageTotal = res.pageTotal || res.data.length;
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$message.warning('该功能尚未开通！')
                // this.$set(this.query, 'pageIndex', 1);
                // this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.delMember(row.userPhone, () => {
                            this.$message.success('删除成功');
                            this.tableData.splice(index, 1);
                        });
                    })
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                this.$message.warning('该功能尚未开通！')
                // const length = this.multipleSelection.length;
                // let str = '';
                // if (length > 0) {
                //     this.delList = this.delList.concat(this.multipleSelection);
                //     for (let i = 0; i < length; i++) {
                //         str += this.multipleSelection[i].name + ' ';
                //     }
                //     this.$message.error(`删除了${str}`);
                //     this.multipleSelection = [];
                // } else {
                //     this.$message.warning(`请先选择删除的成员`);
                // }
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                this.editVisible = false;
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                this.$set(this.tableData, this.idx, this.form);
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            gotoProjectOne() {
                this.$router.push('projectone/overview');
            }
        }
    };
</script>


<style scoped>
    .el-row {
        margin-bottom: 20px;
    }

    .grid-content {
        font-size: 16px;
        display: flex;
        align-items: center;
        height: 35px;
    }

    .grid-cont-right {
        flex: 1;
        text-align: center;
        font-size: 14px;
        color: #999;
    }

    .grid-num {
        font-size: 14px;
        font-weight: bold;
    }

    .grid-con-icon {
        font-size: 40px;
        width: 40px;
        height: 40px;
        text-align: center;
        color: #fff;
    }

    /*.grid-con-1 .grid-con-icon {*/
    /*    font-size: 14px;*/
    /*    color:  #FFD700;*/
    /*}*/

    .grid-con-1 .grid-num {
        color: #FFD700
    }

    /*.grid-con-2 .grid-con-icon {*/
    /*    background: rgb(100, 213, 114);*/
    /*}*/

    .grid-con-2 .grid-num {
        color: rgb(100, 213, 114);
    }

    /*.grid-con-3 .grid-con-icon {*/
    /*    font-size: 14px;*/
    /*    color: #2d8cf0;*/
    /*    !*background: rgb(242, 94, 67);*!*/
    /*}*/

    .grid-con-3 .grid-num {
        color: rgb(45, 140, 240);
    }

    .user-info {
        display: flex;
        align-items: center;
        margin-top: 20px;
        margin-bottom: 20px;
    }

    .user-avator {
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }

    .user-info-cont {
        padding-left: 20px;
        flex: 1;
        font-size: 14px;
        color: #999;
    }

    .user-info-cont div:first-child {
        font-size: 15px;
        color: #222;
    }

    .user-info-list {
        font-size: 14px;
        color: #999;
        line-height: 25px;
    }

    .user-info-list span {
        margin-left: 70px;
    }

    .mgb20 {
        margin-left: 0px !important;
        margin-right: 0px !important;
        margin-top: 6px;
        margin-bottom: 8px;
    }

    .todo-item {
        font-size: 14px;
    }

    .todo-item-del {
        text-decoration: line-through;
        color: #999;
    }

    .schart {
        width: 100%;
        height: 300px;
    }

    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
